package com.ssis.graphics;

import java.io.IOException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.util.Log;
import com.ssis.core.Game;

public class Renderer {
	int width = 800;
	int height = 600;
	Game game;
	Starfield starfield;
	Sprite logo;
	
	public Renderer(Game game) throws IOException {
		this.game = game;

		GL11.glEnable(GL11.GL_TEXTURE_2D);               
        
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);          
        
        // enable alpha blending
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        
        GL11.glViewport(0,0,width,height);
		
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, Display.getDisplayMode().getWidth(),Display.getDisplayMode().getHeight(),  0, -1, 1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	
		// clear the screen
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
		
		starfield = new Starfield(game);
		logo = new Sprite("logo", 0 ,0);
	}
	

	public void render() {
		// clear the screen
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
		try {
			if (game.state == Game.state_types.MAIN_MENU) {
				logo.render();
			}
			if(game.state == Game.state_types.STATION) {
				starfield.render();
				game.station().render();
				game.hudStation().render();
				printCargo();
			}
			if(game.state == Game.state_types.DIALOG) {
				starfield.render();
				game.station().render();
				game.hudStation().render();
				printCargo();
				Dialog.render();
			}
			if(game.state == Game.state_types.CREW_MENU) {
				starfield.render();
				game.station().render();
				game.hudStation().render();
				printCargo();
				game.hudCrew().render();
			}
		} catch (IOException e) {
			Log.debug("Error rendering");
			e.printStackTrace();
		}
		starfield.update();
	}
	
	private void printCargo() {
		game.text().print(700, 550, "DUDES:  " + game.station().crewSize());
		game.text().print(700, 560, "MONEYS: " + game.station().cargo().getCredits());
		game.text().print(700, 570, "POWAH!: " + game.station().cargo().getEnergy());
		game.text().print(700, 580, "      / " + game.station().maxEnergy());
	}
}
